﻿using MvvmCross.Platform;
using MvvmCross.Plugins.Messenger;
using Sidvall.WorkItemManager.UI.Messaging;

namespace Sidvall.WorkItemManager.UI.ViewModels
{
    public partial class ScheduleViewModel
    {
        #region Public Members

        #region Context

        private string _Context;
        public string Context
        {
            get
            {
                return _Context;
            }
            set
            {
                if (_Context == value)
                    return;
                _Context = value;
                RaisePropertyChanged("Context");
                OnContextChanged();
            }
        }

        #endregion
        #region DayFrequencyTypes

        private Sidvall.Data.KeyValueCollection<string, Workflow.Conditions.DayFrequencyType> _DayFrequencyTypes;
        public Sidvall.Data.KeyValueCollection<string, Workflow.Conditions.DayFrequencyType> DayFrequencyTypes
        {
            get
            {
                if (_DayFrequencyTypes == null)
                    this.DayFrequencyTypes = Sidvall.Data.KeyValueManager.FromEnum<Sidvall.WorkItemManager.Workflow.Conditions.DayFrequencyType>();
                return _DayFrequencyTypes;
            }
            set
            {
                _DayFrequencyTypes = value;
                RaisePropertyChanged("DayFrequencyTypes");
            }
        }

        #endregion
        #region MonthFrequencyTypes

        private Sidvall.Data.KeyValueCollection<string, Workflow.Conditions.MonthFrequencyType> _MonthFrequencyTypes;
        public Sidvall.Data.KeyValueCollection<string, Workflow.Conditions.MonthFrequencyType> MonthFrequencyTypes
        {
            get
            {
                if (_MonthFrequencyTypes == null)
                    this.MonthFrequencyTypes = Sidvall.Data.KeyValueManager.FromEnum<Sidvall.WorkItemManager.Workflow.Conditions.MonthFrequencyType>();
                return _MonthFrequencyTypes;
            }
            set
            {
                _MonthFrequencyTypes = value;
                RaisePropertyChanged("MonthFrequencyTypes");
            }
        }

        #endregion
        #region ScheduleFrequencyTypes

        private Sidvall.Data.KeyValueCollection<string, Workflow.Conditions.ScheduleFrequencyType> _ScheduleFrequencyTypes;
        public Sidvall.Data.KeyValueCollection<string, Workflow.Conditions.ScheduleFrequencyType> ScheduleFrequencyTypes
        {
            get
            {
                if (_ScheduleFrequencyTypes == null)
                    this.ScheduleFrequencyTypes = Sidvall.Data.KeyValueManager.FromEnum<Sidvall.WorkItemManager.Workflow.Conditions.ScheduleFrequencyType>();
                return _ScheduleFrequencyTypes;
            }
            set
            {
                _ScheduleFrequencyTypes = value;
                RaisePropertyChanged("ScheduleFrequencyTypes");
            }
        }

        #endregion

        #region OnActiveEntityChanged

        protected override void OnActiveEntityChanged(Sidvall.Business.BusinessObjectChangeMode changeMode, Sidvall.Business.ChildChangedEventArgs childArgs, System.ComponentModel.PropertyChangedEventArgs propertyArgs)
        {
            this.Context = Sidvall.WorkItemManager.UI.Entities.Schedule.Serialize(this.ActiveEntity);
        }

        #endregion
        #region OnContextChanged

        protected void OnContextChanged()
        {
            if (this.PublishChange)
            {
                var messenger = Mvx.Resolve<IMvxMessenger>();
                var message = new WorkItemConditionContextMessage(this)
                {
                    Context = this.Context,
                };
                messenger.Publish<WorkItemConditionContextMessage>(message);
            }
        }

        #endregion

        #region Init

        public void Init(ContextParameter item)
        {
            this.ContextParameter = item;
        }

        #endregion
        #region Start

        public override void Start()
        {
            var messenger = Mvx.Resolve<IMvxMessenger>();
            this.ViewModelResources.Add(messenger.Subscribe<WorkItemConditionContextMessage>(OnWorkItemConditionContextMessage));

            if (this.ContextParameter != null)
            {
                this.PublishChange = false;
                this.Context = this.ContextParameter.Context;
                this.PublishChange = true;
                this.ActiveEntity = Sidvall.WorkItemManager.UI.Entities.Schedule.Deserialize(this.Context);
                this.ContextParameter = null;
            }
        }

        #endregion

        #endregion
        #region Private Members

        private ContextParameter ContextParameter { get; set; }
        private bool PublishChange { get; set; }

        #region OnWorkItemConditionContextMessage

        private void OnWorkItemConditionContextMessage(WorkItemConditionContextMessage item)
        {
            if (this.Context == item.Context)
                return;
            this.PublishChange = false;
            this.Context = item.Context;
            this.PublishChange = true;
            this.ActiveEntity = Sidvall.WorkItemManager.UI.Entities.Schedule.Deserialize(this.Context);
        }

        #endregion

        #endregion
    }
}
